// pages/index/index.js
const common = require('../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowDate: '',
        popImg: '',
        showAdv: true,
        isShow: false,
        isShowData: {},
        userInfo: {},
        isMemeber: true,
        guessType: '',
        guessLikeClass: [],
        guessLikeList: [],
        brandList: [],
        brandGoodsList: [],
        imgSwiper: [],
        teacherList: [],
        voiceCourseList: [],
        offlineCourseList: [],
        starList: [],
        articalList: [],
        videoPlayIndex: null,
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.userShare) {
            wx.setStorageSync('userShare', options.userShare)
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: options.userShare
                }
                common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
            }
        }
        if (wx.getStorageSync('token')) {
            this.getUserInfo()
        }
        this.setData({
            nowDate: new Date().getTime()
        })
        this.getPopImg()
        this.getIndexShow()
        this.getHomeInfo()
        this.getImgSwiper()
        this.getTeacherList()
        this.getVoiceCourse()
        this.getOfflineCourse()
        this.getBrandList()
        this.getStarList()
        this.getArticalList()
        // this.getGuessLikeList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        this.setData({
            isLoading: false
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            articalList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getArticalList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        let userId = wx.getStorageSync('userId')
        console.log(userId)
        if (userId) {
            return {
                title: '财大师',
                path: '/pages/index/index?userShare=' + userId,
                imageUrl: 'https://www.meiboshi.vip/static/images/wx_sm/home/be-vip-img.jpg'
            }
        }
    },

    getPopImg: function() {
        const that = this
        wx.showLoading({
            title: '加载中'
        })
        let data = {
            activity: 785,
            images_id: 9
        }
        common.post('Images/get_img', data).then(function(res) {
            wx.hideLoading()
            that.setData({
                popImg: res[0]
            })
        }).catch(function(e) {})
    },

    getIndexShow: function() {
        const that = this
        common.post('Home/get_index3', {
            activity: 785
        }).then(function(res) {
            if (res.code == 204) {
                return
            }
            if (res.show.is_show == app.globalData.isShow) {
                that.setData({
                    isShow: true
                })
            } else {
                that.setData({
                    isShow: false
                })
            }
        }).catch(function(e) {})
    },

    getHomeInfo: function() {
        const that = this
        let data = {
            activity: 785
        }
        common.post('Home/index', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    guessLikeClass: res.all_class
                })
            }
        }).catch(function(e) {})
    },

    getUserInfo: function() {
        const that = this
        if (wx.getStorageSync('token')) {
            let data = {
                isStore: 1
            }
            common.post('User/user_info', data).then(function(res) {
                if (res.code == 204) {
                    return
                } else {
                    that.setData({
                        userInfo: res
                    })
                }
            }).catch(function(e) {})
        }
    },

    getImgSwiper: function() {
        const that = this
        let data = {
            client: 2,
            activity: 785
        }
        common.post('Slideshow/top_slideshow', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    imgSwiper: []
                })
            } else {
                that.setData({
                    imgSwiper: res
                })
            }
        }).catch(function(e) {})
    },

    getTeacherList: function() {
        const that = this
        let data = {
            page: 0,
            limit: 20,
            order_by: 'sort',
            activity: 785
        }
        common.post('Online/get_online_teacher', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    teacherList: []
                })
            } else {
                that.setData({
                    teacherList: res
                })
            }
        }).catch(function(e) {})
    },

    getVoiceCourse: function() {
        const that = this
        let data = {
            page: 0,
            limit: 20,
            activity: 785
        }
        common.post('Online/online_list', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    voiceCourseList: []
                })
            } else {
                that.setData({
                    voiceCourseList: res
                })
            }
        }).catch(function(e) {})
    },

    getOfflineCourse: function() {
        const that = this
        let data = {
            page: 0,
            limit: 20,
            activity: 785
        }
        common.post('Online/down_online_list', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    offlineCourseList: []
                })
            } else {
                that.setData({
                    offlineCourseList: res
                })
            }
        }).catch(function(e) {})
    },

    getBrandList: function() {
        const that = this
        let data = {
            type: 8,
            activity: 785
        }
        common.post('Shelf/Shelf', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    brandList: []
                })
            } else {
                let brandGoodsList = that.data.brandGoodsList
                that.setData({
                    brandList: res
                })
                that.data.brandList.forEach((item, i) => {
                    let data = {
                        shelf_id: item.id
                    }
                    common.post('Shelf/Shelf_list', data).then(function(res) {
                        if (res.code == 204) {
                            brandGoodsList[i] = []
                        } else {
                            brandGoodsList[i] = res
                        }
                        that.setData({
                            brandGoodsList: brandGoodsList
                        })
                    }).catch(function(e) {})
                })
            }
        }).catch(function(e) {})
    },

    getStarList: function() {
        const that = this
        let starList = this.data.starList
        let data = {
            page: 0,
            limit: 20,
            activity: 785
        }
        common.post('Redman/get_redman', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    starList: []
                })
            } else {
                that.setData({
                    starList: res
                })
            }
        }).catch(function(e) {})
    },

    getArticalList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let articalList = this.data.articalList
        let data = {
            activity: 785,
            limit: 10,
            order_by: 'sort',
            page: this.data.nowPage
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Moments/moment_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    articalList: articalList,
                    isAll: true
                })
            } else {
                articalList = articalList.concat(res)
                that.setData({
                    articalList: articalList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (this.data.articalType == e.detail) {
            return
        }
        if (this.data.isLoading) {
            return
        }
        this.setData({
            nowPage: 0,
            isAll: false,
            isLoading: false,
            articalList: []
        })
        this.getArticalList()
    },

    praiseClick: function(e) {
        const that = this
        let articalList = this.data.articalList
        if (e.detail.status == 1) {
            return
        }
        let data = {
            moments_id: e.detail.id
        }
        common.post('Moments/moment_like', data).then(function(res) {
            articalList.forEach(function(item) {
                if (item.id == e.detail.id) {
                    item.is_like = 1
                    item.praise_num = 1 + parseInt(item.praise_num)
                }
            })
            that.setData({
                articalList: articalList
            })
        }).catch(function(e) {})
    },

    getGuessLikeList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let guessLikeList = this.data.guessLikeList
        let data = {
            page: this.data.nowPage,
            limit: 10,
            class_id: this.data.guessType,
            is_goods: 0,
            activity: 785
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    guessLikeList: guessLikeList,
                    isAll: true
                })
            } else {
                guessLikeList = guessLikeList.concat(res)
                that.setData({
                    guessLikeList: guessLikeList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    guessClick: function(e) {
        if (this.data.guessType == e.detail) {
            return
        }
        this.setData({
            guessType: e.detail,
            nowPage: 0,
            isAll: false,
            isLoading: false
        })
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let guessLikeList = this.data.guessLikeList
        let data = {
            page: this.data.nowPage,
            limit: 10,
            class_id: this.data.guessType,
            activity: 785
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                guessLikeList = []
                that.setData({
                    guessLikeList: guessLikeList,
                    isAll: true
                })
            } else {
                guessLikeList = []
                guessLikeList = guessLikeList.concat(res)
                that.setData({
                    guessLikeList: guessLikeList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    closeAdv: function() {
        this.setData({
            showAdv: false
        })
    },
    donothing: function () {
        return
    },
    videoPlay: function (e) {
        if (this.data.videoPlayIndex == null || this.data.videoPlayIndex == e.detail) {
            this.setData({
                videoPlayIndex: e.detail
            })
        } else {
            let videoContextPrev = wx.createVideoContext('video' + this.data.videoPlayIndex)
            videoContextPrev.pause()
            this.setData({
                videoPlayIndex: e.detail
            })
        }
    }
})